/**
 * Description:
 * User: Vijay <1937832819@qq.com>
 * Date: 2020/08/19
 * Time: 11:14
 */

package controllers

import (
	"github.com/astaxie/beego"
	"strings"
	"time"
)

type BaseController struct {
	beego.Controller
}

/**
状态码
*/
const (
	CODE_OK  = 0 //表示没有错误
	CODE_ERR = 1 // 存在错误
)

//前期准备
func (self *BaseController) Prepare() {

}

// 是否POST提交
func (self *BaseController) IsPost() bool {
	return self.Ctx.Request.Method == "POST"
}

//获取用户IP地址
func (self *BaseController) GetClientIp() string {
	s := self.Ctx.Request.RemoteAddr
	l := strings.LastIndex(s, ":")
	return s[0:l]
}

//ajax返回
func (self *BaseController) ResJson(code int, msg interface{}, data interface{}, args map[string]interface{}) {
	out := make(map[string]interface{})
	out["code"] = code
	out["msg"] = msg
	out["data"] = data
	for key, val := range args {
		out[key] = val
	}
	//beego.Date(time.Unix(time.Now(), 0), "Y-m-d H:i:s")
	//out["create_time"]  = time.Now().Format("2006-01-02 15:04:05")
	out["create_time"] = beego.Date(time.Now(), "Y-m-d H:i:s")
	self.Data["json"] = out
	self.ServeJSON()
	self.StopRun()
}
